<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:p="pilferpage"
	xmlns:html="http://www.w3.org/1999/xhtml"
	xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	>

	<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

	<xsl:template match="/">
		<window>
			<xsl:apply-templates/>
		</window>
	</xsl:template>

	<xsl:template match="p:table">
		<!-- <xsl:apply-templates select="p:title"/> -->
		<xsl:apply-templates select="p:title"/>
		<xsl:choose>
			<xsl:when test="descendant::p:row/p:row">
				 <tree flex="1">
					<treecols>
						<xsl:choose>
							<xsl:when test="p:head">
								<xsl:for-each select="p:head/p:row[1]/p:cell">
									<treecol label="{normalize-space(.)}" flex="1">
										<xsl:if test="position() = 1"><xsl:attribute name="primary">true</xsl:attribute></xsl:if>
									</treecol>
								</xsl:for-each>
							</xsl:when>
							<xsl:when test="p:body">
								<xsl:for-each select="p:body/p:row[1]/p:cell">
									<treecol label="Column {position()}" flex="1">
										<xsl:if test="position() = 1"><xsl:attribute name="primary">true</xsl:attribute></xsl:if>
									</treecol>
								</xsl:for-each>
							</xsl:when>
							<xsl:when test="p:row">
								<xsl:for-each select="p:row[1]/p:cell">
									<treecol label="Column {position()}" flex="1">
										<xsl:if test="position() = 1"><xsl:attribute name="primary">true</xsl:attribute></xsl:if>
									</treecol>
								</xsl:for-each>
							</xsl:when>
						</xsl:choose>
					</treecols>
					<treechildren>
						<xsl:apply-templates select="p:row | p:body" mode="tree"/>
					</treechildren>
				</tree>
			</xsl:when>
			<xsl:otherwise>
				<listbox>
					<xsl:apply-templates select="p:head" mode="listbox"/>
					<xsl:apply-templates select="p:row | p:body" mode="listbox"/>
				</listbox>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="p:row" mode="listbox">
		<listitem>
			<xsl:apply-templates select="p:cell" mode="listbox"/>	
		</listitem>
		<xsl:apply-templates select="p:row"/>
	</xsl:template>

	<xsl:template match="p:row" mode="tree">
		<treeitem container="true" open="true">
			<treerow>
				<xsl:apply-templates select="p:cell" mode="tree"/>
			</treerow>
			<xsl:if test="p:row">
				<treechildren>
					<xsl:apply-templates select="p:row" mode="tree"/>
				</treechildren>
			</xsl:if>
		</treeitem>
		<xsl:text>&#9;&#10;</xsl:text>
	</xsl:template>

	<xsl:template match="p:cell" mode="listbox">
		<xsl:choose>
			<xsl:when test="ancestor::p:head">
				<listheader label="{normalize-space(.)}"/>
			</xsl:when>
			<xsl:otherwise>
				<listcell><xsl:apply-templates/></listcell>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="p:cell" mode="tree">
		<treecell label="{normalize-space(.)}"/>
	</xsl:template>

	<xsl:template match="p:paragraph">
		<xsl:element name="html:p">
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

	<xsl:template match="p:link">
		<xsl:element name="html:a">
			<xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>


	<xsl:template match="p:title">
		<xsl:variable name="ancestorSections" select="ancestor::p:section"/>
		<xsl:choose>
			<xsl:when test="count($ancestorSections) &gt; 4">
				<html:h6><xsl:apply-templates/></html:h6>
			</xsl:when>
			<xsl:when test="count($ancestorSections) &gt; 3">
				<html:h5><xsl:apply-templates/></html:h5>
			</xsl:when>
			<xsl:when test="count($ancestorSections) &gt; 2">
				<html:h4><xsl:apply-templates/></html:h4>
			</xsl:when>
			<xsl:when test="count($ancestorSections) &gt; 1">
				<html:h3><xsl:apply-templates/></html:h3>
			</xsl:when>
			<xsl:when test="count($ancestorSections) &gt; 0">
				<html:h2><xsl:apply-templates/></html:h2>
			</xsl:when>
			<xsl:otherwise>
				<html:h1><xsl:apply-templates/></html:h1>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>	


</xsl:stylesheet>
